 package handling.world;
 import client.MapleCharacter;
 import client.inventory.Item;
 import com.alibaba.druid.pool.DruidPooledConnection;
 import database.DatabaseConnection;
 import handling.Auction.AuctionServer;
 import handling.cashshop.CashShopServer;
 import handling.channel.ChannelServer;
 import handling.channel.PlayerStorage;
 import handling.world.party.ExpeditionType;
 import handling.world.party.MapleExpedition;
 import handling.world.party.MapleParty;
 import handling.world.party.MaplePartyCharacter;
 import handling.world.party.PartySearch;
 import handling.world.party.PartySearchType;
 import java.sql.PreparedStatement;
 import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.EnumMap;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import tools.MaplePacketCreator;
 import tools.packet.PartyPacket;

 public class WorldPartyService {
/*  32 */   private static final Logger log = LogManager.getLogger(WorldPartyService.class.getName());
   private final Map<Integer, MapleParty> partyList;
   private final Map<Integer, MapleExpedition> expedsList;
   private final Map<PartySearchType, List<PartySearch>> searcheList;
   private final AtomicInteger runningPartyId;
   private final AtomicInteger runningExpedId;
   private final ReentrantReadWriteLock lock;

   private WorldPartyService() {
/*  41 */     log.info("正在启动[WorldPartyService]");
/*  42 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/*  43 */       PreparedStatement ps = con.prepareStatement("UPDATE characters SET party = -1, fatigue = 0");
/*  44 */       ps.executeUpdate();
/*  45 */       ps.close();
/*  46 */     } catch (SQLException e) {
/*  47 */       log.error("更新角色组队为-1失败...", e);
     }
/*  49 */     this.lock = new ReentrantReadWriteLock();
/*  50 */     this.runningPartyId = new AtomicInteger(1);
/*  51 */     this.runningExpedId = new AtomicInteger(1);
/*  52 */     this.partyList = new HashMap<>();
/*  53 */     this.expedsList = new HashMap<>();
/*  54 */     this.searcheList = new EnumMap<>(PartySearchType.class);
/*  55 */     for (PartySearchType pst : PartySearchType.values()) {
/*  56 */       this.searcheList.put(pst, new ArrayList<>());
     }
   }

   public static WorldPartyService getInstance() {
/*  61 */     return SingletonHolder.instance;
   }











   public void expedChat(boolean itemchat, int expedId, String chatText, String nameFrom, Item item) {
/*  75 */     MapleExpedition expedition = getExped(expedId);
/*  76 */     if (expedition == null) {
       return;
     }
/*  79 */     for (Iterator<Integer> iterator = expedition.getParties().iterator(); iterator.hasNext(); ) { int i = ((Integer)iterator.next()).intValue();
/*  80 */       partyChat(itemchat, i, chatText, nameFrom, 4, item); }

   }




   public void sendExpedPacket(int expedId, byte[] packet, MaplePartyCharacter exception) {
/*  88 */     MapleExpedition expedition = getExped(expedId);
/*  89 */     if (expedition == null) {
       return;
     }
/*  92 */     for (Iterator<Integer> iterator = expedition.getParties().iterator(); iterator.hasNext(); ) { int i = ((Integer)iterator.next()).intValue();
/*  93 */       sendPartyPacket(i, packet, exception); }

   }




   public void sendPartyPacket(int partyId, byte[] packet, MaplePartyCharacter exception) {
/* 101 */     MapleParty party = getParty(partyId);
/* 102 */     if (party == null) {
       return;
     }
/* 105 */     for (MaplePartyCharacter partychar : party.getMembers()) {
/* 106 */       int ch = WorldFindService.getInstance().findChannel(partychar.getName());
/* 107 */       if (ch > 0 && (exception == null || partychar.getId() != exception.getId())) {
/* 108 */         MapleCharacter player = ChannelServer.getInstance(ch).getPlayerStorage().getCharacterByName(partychar.getName());
/* 109 */         if (player != null) {
/* 110 */           player.getClient().announce(packet);
         }
       }
     }
   }




   public void partyChat(boolean itemPartyChat, int partyId, String chatText, String nameFrom, Item item) {
/* 120 */     partyChat(itemPartyChat, partyId, chatText, nameFrom, 1, item);
   }




   public void partyChat(boolean ItemPartyChat, int partyId, String chatText, String nameFrom, int mode, Item item) {
/* 127 */     MapleParty party = getParty(partyId);
/* 128 */     if (party == null) {
       return;
     }
/* 131 */     for (MaplePartyCharacter partychar : party.getMembers()) {
/* 132 */       int ch = WorldFindService.getInstance().findChannel(partychar.getName());
/* 133 */       if (ch > 0) {
/* 134 */         MapleCharacter player = ChannelServer.getInstance(ch).getPlayerStorage().getCharacterByName(partychar.getName());
/* 135 */         if (player != null && !player.getName().equalsIgnoreCase(nameFrom)) {
/* 136 */           if (ItemPartyChat) {
/* 137 */             player.getClient().announce(MaplePacketCreator.multiChat(true, nameFrom, chatText, mode, item));
           } else {
/* 139 */             player.getClient().announce(MaplePacketCreator.multiChat(nameFrom, chatText, mode));
           }
/* 141 */           if (player.getClient().isMonitored()) {
/* 142 */             WorldBroadcastService.getInstance().broadcastGMMessage(MaplePacketCreator.serverWithoutPrefix("[GM消息] " + nameFrom + " said to " + player.getName() + " (组队): " + chatText));
           }
         }
       }
     }
   }





   public void partyMessage(int partyId, String chatText) {
/* 154 */     MapleParty party = getParty(partyId);
/* 155 */     if (party == null) {
       return;
     }
/* 158 */     for (MaplePartyCharacter partychar : party.getMembers()) {
/* 159 */       int ch = WorldFindService.getInstance().findChannel(partychar.getName());
/* 160 */       if (ch > 0) {
/* 161 */         MapleCharacter player = ChannelServer.getInstance(ch).getPlayerStorage().getCharacterByName(partychar.getName());
/* 162 */         if (player != null) {
/* 163 */           player.dropMessage(5, chatText);
         }
       }
     }
   }




   public void expedMessage(int expedId, String chatText) {
/* 173 */     MapleExpedition expedition = getExped(expedId);
/* 174 */     if (expedition == null) {
       return;
     }
/* 177 */     for (Iterator<Integer> iterator = expedition.getParties().iterator(); iterator.hasNext(); ) { int i = ((Integer)iterator.next()).intValue();
/* 178 */       partyMessage(i, chatText); }

   }




   public void updatePartySetup(int partyId, PartyOperation operation, String partyName, boolean isHidden) {
/* 186 */     MapleParty party = getParty(partyId);
/* 187 */     if (party == null) {
/* 188 */       System.out.println("no party with the specified partyid exists.");
       return;
     }
/* 191 */     party.setName(partyName);
/* 192 */     party.setHidden(isHidden);
/* 193 */     if (party.getMembers().size() <= 0) {
/* 194 */       disbandParty(partyId);
     }
/* 196 */     for (MaplePartyCharacter partychar : party.getMembers()) {
/* 197 */       if (partychar == null) {
         continue;
       }
/* 200 */       int ch = WorldFindService.getInstance().findChannel(partychar.getName());
/* 201 */       if (ch > 0) {
/* 202 */         MapleCharacter chr = getStorage(ch).getCharacterByName(partychar.getName());
/* 203 */         if (chr != null) {
/* 204 */           chr.setParty(party);
/* 205 */           chr.send(PartyPacket.updateParty(chr.getClient().getChannel(), party, operation, null));
         }
       }
     }
   }




   public void updateParty(int partyId, PartyOperation operation, MaplePartyCharacter target) {
/* 215 */     MapleParty party = getParty(partyId);
/* 216 */     if (party == null) {
/* 217 */       System.out.println("no party with the specified partyid exists.");
       return;
     }
/* 220 */     int oldExped = party.getExpeditionId();
/* 221 */     int oldIndex = -1;
/* 222 */     if (oldExped > 0) {
/* 223 */       MapleExpedition exped = getExped(oldExped);
/* 224 */       if (exped != null) {
/* 225 */         oldIndex = exped.getIndex(partyId);
       }
     }
/* 228 */     switch (operation) {
       case 加入队伍:
/* 230 */         party.addMember(target);
/* 231 */         if (party.getMembers().size() >= 6) {
/* 232 */           PartySearch toRemove = getSearchByParty(partyId);
/* 233 */           if (toRemove != null) {
/* 234 */             removeSearch(toRemove, "队伍人数已满，组队广告已被删除。"); break;
/* 235 */           }  if (party.getExpeditionId() > 0) {
/* 236 */             MapleExpedition exped = getExped(party.getExpeditionId());
/* 237 */             if (exped != null && exped.getAllMembers() >= (exped.getType()).maxMembers) {
/* 238 */               toRemove = getSearchByExped(exped.getId());
/* 239 */               if (toRemove != null) {
/* 240 */                 removeSearch(toRemove, "队伍人数已满，组队广告已被删除。");
               }
             }
           }
         }
         break;
       case 驱逐成员:
       case 离开队伍:
/* 248 */         party.removeMember(target);
         break;
       case 解散队伍:
/* 251 */         disbandParty(partyId);
         break;
       case 更新队伍:
       case LOG_ONOFF:
/* 255 */         party.updateMember(target);
         break;
       case 改变队长:
       case CHANGE_LEADER_DC:
/* 259 */         party.setLeader(target);
         break;
       default:
/* 262 */         throw new RuntimeException("Unhandeled updateParty operation " + operation.name());
     }
/* 264 */     if (operation == PartyOperation.离开队伍 || operation == PartyOperation.驱逐成员) {
/* 265 */       int chz = WorldFindService.getInstance().findChannel(target.getName());
/* 266 */       if (chz > 0) {
/* 267 */         MapleCharacter player = getStorage(chz).getCharacterByName(target.getName());
/* 268 */         if (player != null) {
/* 269 */           player.setParty(null);
/* 270 */           if (oldExped > 0) {
/* 271 */             player.getClient().announce(PartyPacket.expeditionMessage(false));
           }
/* 273 */           player.getClient().announce(PartyPacket.updateParty(player.getClient().getChannel(), party, operation, target));
         }
       }
/* 276 */       if (target.getId() == party.getLeader().getId() && party.getMembers().size() > 0) {
/* 277 */         MaplePartyCharacter lchr = null;
/* 278 */         for (MaplePartyCharacter pchr : party.getMembers()) {
/* 279 */           if (pchr != null && (lchr == null || lchr.getLevel() < pchr.getLevel())) {
/* 280 */             lchr = pchr;
           }
         }
/* 283 */         if (lchr != null) {
/* 284 */           updateParty(partyId, PartyOperation.CHANGE_LEADER_DC, lchr);
         }
       }
     }
/* 288 */     if (party.getMembers().size() <= 0) {
/* 289 */       disbandParty(partyId);
     }
/* 291 */     for (MaplePartyCharacter partychar : party.getMembers()) {
/* 292 */       if (partychar == null) {
         continue;
       }
/* 295 */       int ch = WorldFindService.getInstance().findChannel(partychar.getName());
/* 296 */       if (ch > 0) {
/* 297 */         MapleCharacter chr = getStorage(ch).getCharacterByName(partychar.getName());
/* 298 */         if (chr != null) {
/* 299 */           if (operation == PartyOperation.解散队伍) {
/* 300 */             chr.setParty(null);
/* 301 */             if (oldExped > 0) {
/* 302 */               chr.send(PartyPacket.expeditionMessage(true));
             }
           } else {
/* 305 */             chr.setParty(party);
           }
/* 307 */           chr.send(PartyPacket.updateParty(chr.getClient().getChannel(), party, operation, target));
         }
       }
     }
/* 311 */     if (oldExped > 0) {
/* 312 */       sendExpedPacket(oldExped, PartyPacket.expeditionUpdate(oldIndex, party), (operation == PartyOperation.LOG_ONOFF || operation == PartyOperation.更新队伍) ? target : null);
     }
   }




   public MapleParty createParty(MaplePartyCharacter chrfor) {
/* 320 */     return createParty(chrfor, "快去组队游戏吧，GoGo", false);
   }

   public MapleParty createParty(MaplePartyCharacter chrfor, String partyName, boolean isHidden) {
/* 324 */     MapleParty party = new MapleParty(this.runningPartyId.getAndIncrement(), chrfor, partyName, isHidden);
/* 325 */     this.partyList.put(Integer.valueOf(party.getPartyId()), party);
/* 326 */     return party;
   }




   public MapleParty createParty(MaplePartyCharacter chrfor, int expedId) {
/* 333 */     ExpeditionType ex = ExpeditionType.getById(expedId);
/* 334 */     MapleParty party = new MapleParty(this.runningPartyId.getAndIncrement(), chrfor, (ex != null) ? this.runningExpedId.getAndIncrement() : -1);
/* 335 */     this.partyList.put(Integer.valueOf(party.getPartyId()), party);
/* 336 */     if (ex != null) {
/* 337 */       MapleExpedition expedition = new MapleExpedition(ex, chrfor.getId(), party.getExpeditionId());
/* 338 */       expedition.getParties().add(Integer.valueOf(party.getPartyId()));
/* 339 */       this.expedsList.put(Integer.valueOf(party.getExpeditionId()), expedition);
     }
/* 341 */     return party;
   }

   public MapleParty createPartyAndAdd(MaplePartyCharacter chrfor, int expedId) {
/* 345 */     MapleExpedition expedition = getExped(expedId);
/* 346 */     if (expedition == null) {
/* 347 */       return null;
     }
/* 349 */     MapleParty party = new MapleParty(this.runningPartyId.getAndIncrement(), chrfor, expedId);
/* 350 */     this.partyList.put(Integer.valueOf(party.getPartyId()), party);
/* 351 */     expedition.getParties().add(Integer.valueOf(party.getPartyId()));
/* 352 */     return party;
   }




   public MapleParty getParty(int partyId) {
/* 359 */     return this.partyList.get(Integer.valueOf(partyId));
   }




   public MapleParty getPartyByLeaderId(int leaderId) {
/* 366 */     this.lock.readLock().lock();
     try {
/* 368 */       Iterator<MapleParty> itr = this.partyList.values().iterator();

/* 370 */       while (itr.hasNext()) {
/* 371 */         MapleParty party = itr.next();
/* 372 */         if (party != null && party.getLeader().getId() == leaderId) {
/* 373 */           return party;
         }
       }
     } finally {
/* 377 */       this.lock.readLock().unlock();
     }
/* 379 */     return null;
   }

   public MapleExpedition getExped(int partyId) {
/* 383 */     return this.expedsList.get(Integer.valueOf(partyId));
   }




   public MapleExpedition disbandExped(int partyId) {
/* 390 */     PartySearch toRemove = getSearchByExped(partyId);
/* 391 */     if (toRemove != null) {
/* 392 */       removeSearch(toRemove, "远征队解散，组队广告已被删除。");
     }
/* 394 */     MapleExpedition ret = this.expedsList.remove(Integer.valueOf(partyId));
/* 395 */     if (ret != null) {
/* 396 */       for (Iterator<Integer> iterator = ret.getParties().iterator(); iterator.hasNext(); ) { int p = ((Integer)iterator.next()).intValue();
/* 397 */         MapleParty pp = getParty(p);
/* 398 */         if (pp != null) {
/* 399 */           updateParty(p, PartyOperation.解散队伍, pp.getLeader());
         } }

     }
/* 403 */     return ret;
   }




   public MapleParty disbandParty(int partyId) {
/* 410 */     PartySearch toRemove = getSearchByParty(partyId);
/* 411 */     if (toRemove != null) {
/* 412 */       removeSearch(toRemove, "组队解散，组队广告已被删除。");
     }
/* 414 */     MapleParty ret = this.partyList.remove(Integer.valueOf(partyId));
/* 415 */     if (ret == null) {
/* 416 */       return null;
     }
/* 418 */     if (ret.getExpeditionId() > 0) {
/* 419 */       MapleExpedition expedition = getExped(ret.getExpeditionId());
/* 420 */       if (expedition != null) {
/* 421 */         int index = expedition.getIndex(partyId);
/* 422 */         if (index >= 0) {
/* 423 */           expedition.getParties().remove(index);
/* 424 */           sendExpedPacket(expedition.getId(), PartyPacket.expeditionUpdate(index, null), null);
         }
       }
     }
/* 428 */     ret.disband();
/* 429 */     return ret;
   }




   public List<PartySearch> searchParty(PartySearchType pst) {
/* 436 */     return this.searcheList.get(pst);
   }




   public void removeSearch(PartySearch ps, String text) {
/* 443 */     List<PartySearch> ss = this.searcheList.get(ps.getType());
/* 444 */     if (ss.contains(ps)) {
/* 445 */       ss.remove(ps);
/* 446 */       ps.cancelRemoval();
/* 447 */       if ((ps.getType()).exped) {
/* 448 */         expedMessage(ps.getId(), text);
/* 449 */         sendExpedPacket(ps.getId(), PartyPacket.removePartySearch(ps), null);
       } else {
/* 451 */         partyMessage(ps.getId(), text);
/* 452 */         sendPartyPacket(ps.getId(), PartyPacket.removePartySearch(ps), null);
       }
     }
   }




   public void addSearch(PartySearch ps) {
/* 461 */     ((List<PartySearch>)this.searcheList.get(ps.getType())).add(ps);
   }




   public PartySearch getSearch(MapleParty party) {
/* 468 */     for (List<PartySearch> ps : this.searcheList.values()) {
/* 469 */       for (PartySearch p : ps) {
/* 470 */         if ((p.getId() == party.getPartyId() && !(p.getType()).exped) || (p.getId() == party.getExpeditionId() && (p.getType()).exped)) {
/* 471 */           return p;
         }
       }
     }
/* 475 */     return null;
   }




   public PartySearch getSearchByParty(int partyId) {
/* 482 */     for (List<PartySearch> ps : this.searcheList.values()) {
/* 483 */       for (PartySearch p : ps) {
/* 484 */         if (p.getId() == partyId && !(p.getType()).exped) {
/* 485 */           return p;
         }
       }
     }
/* 489 */     return null;
   }




   public PartySearch getSearchByExped(int partyId) {
/* 496 */     for (List<PartySearch> ps : this.searcheList.values()) {
/* 497 */       for (PartySearch p : ps) {
/* 498 */         if (p.getId() == partyId && (p.getType()).exped) {
/* 499 */           return p;
         }
       }
     }
/* 503 */     return null;
   }




   public boolean partyListed(MapleParty party) {
/* 510 */     return (getSearchByParty(party.getPartyId()) != null);
   }

   public PlayerStorage getStorage(int channel) {
/* 514 */     if (channel == -20)
/* 515 */       return AuctionServer.getPlayerStorage(); 
/* 516 */     if (channel == -10) {
/* 517 */       return CashShopServer.getPlayerStorage();
     }
/* 519 */     return ChannelServer.getInstance(channel).getPlayerStorage();
   }

   private static class SingletonHolder
   {
/* 524 */     protected static final WorldPartyService instance = new WorldPartyService();
   }
 }


/* Location:              C:\PGX\Server.jar!\handling\world\WorldPartyService.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */